package cn.tedu.shoppingmall.controller;

import cn.tedu.shoppingmall.mapper.CommodityMapper;
import cn.tedu.shoppingmall.mapper.ShopMapper;
import cn.tedu.shoppingmall.mapper.TransactionMapper;
import cn.tedu.shoppingmall.mapper.UserMapper;
import cn.tedu.shoppingmall.vo.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "4订单模块")
@RestController
@RequestMapping("/trans")
public class TransactionController {
    @Autowired(required = true)
    TransactionMapper transactionMapper;
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    UserMapper userMapper;
    @PostMapping("/deleteOne")
    public String deleteTransactionOne(int id){
        int i = transactionMapper.deleteTransactionOne(id);
        if (i==1){
            return "ok";
        }
        return "删除失败,出现异常!";
    }
    //查询单个订单详情
    @GetMapping("/One")
    public Transaction getTransOne(int transid){

        Transaction transaction = transactionMapper.getTransOne(transid);

        //获得该订单所有商品信息
        List<ComTrans> com = transactionMapper.getComTransAllByTransUUID(transaction.getUUID());
        //加入进去
        transaction.setComTrans(com);
        return transaction;
    }

    //查询登录用户所有订单信息
    @GetMapping("/All")
    public List<Transaction> getTransAll(@AuthenticationPrincipal UserDetails userDetails){
        User user = userMapper.selectByUsername(userDetails.getUsername());
        List<Transaction> transactions =new ArrayList<>();
        for (Transaction t : transactionMapper.getTransAll(user.getId())){
//            //定义一个数组存商品信息
//            List<Commodity> comTrans = new ArrayList<>();
            //获得该订单所有商品信息
            List<ComTrans> com = transactionMapper.getComTransAllByTransUUID(t.getUUID());
            //加入进去
            t.setComTrans(com);
            transactions.add(t);
        }
        return transactions;
    }

    //查询登录用户所有订单信息
    @GetMapping("/AllOne")
    public List<Transaction> getTransAll(String username){
        User user = userMapper.selectByUsername(username);
        List<Transaction> transactions =new ArrayList<>();
        for (Transaction t : transactionMapper.getTransAll(user.getId())){
//            //定义一个数组存商品信息
//            List<Commodity> comTrans = new ArrayList<>();
            //获得该订单所有商品信息
            List<ComTrans> com = transactionMapper.getComTransAllByTransUUID(t.getUUID());
            //加入进去
            t.setComTrans(com);
            transactions.add(t);
        }
        return transactions;
    }

    //查询所有订单信息 --->并分页
    @GetMapping("/AllsLimit")
    public List<Transaction> getTransAllsLimit(Integer pageNum){
        List<UserDetail> users = userMapper.selectUserDetailAll();
        List<Transaction> transactions =new ArrayList<>();
        for (UserDetail u: users){
            for (Transaction t : transactionMapper.getTransAll(u.getId())){
//            //定义一个数组存商品信息
//            List<Commodity> comTrans = new ArrayList<>();
                //获得该订单所有商品信息
                List<ComTrans> com = transactionMapper.getComTransAllByTransUUID(t.getUUID());
                //加入进去
                t.setComTrans(com);
                transactions.add(t);
            }
        }
        return transactions;
    }

    //查询所有订单信息
    @GetMapping("/Alls")
    public List<Transaction> getTransAlls(@AuthenticationPrincipal UserDetails userDetails){
        List<UserDetail> users = userMapper.selectUserDetailAll();
        List<Transaction> transactions =new ArrayList<>();
        for (UserDetail u: users){
            for (Transaction t : transactionMapper.getTransAll(u.getId())){
//            //定义一个数组存商品信息
//            List<Commodity> comTrans = new ArrayList<>();
                //获得该订单所有商品信息
                List<ComTrans> com = transactionMapper.getComTransAllByTransUUID(t.getUUID());
                //加入进去
                t.setComTrans(com);
                transactions.add(t);
            }
        }
        return transactions;
    }


    //查询管理首页的销售额,今日销售额,销售数量
    @GetMapping("/selectToComMoney")
    public Map<String,Integer> getComMoney(){
        String createdDay = String.valueOf(LocalDate.now());
        String created = String.valueOf(LocalDateTime.now());
        // 获取今天的日期
        LocalDateTime today = LocalDateTime.now();
        // 取本月第1天：
        String firstDayOfThisMonth = String.valueOf(today.with(TemporalAdjusters.firstDayOfMonth()));
        // 取本年第1天：
        String firstDayOfYear = String.valueOf(today.with(TemporalAdjusters.firstDayOfYear()));

        Map<String,Integer> moneys = new HashMap<>();
        moneys.put("commodityMoneyAll",transactionMapper.SelectByComPriceAll());
        moneys.put("commoditySumAll",transactionMapper.SelectByComSumAll());
        moneys.put("commoditySumDay",transactionMapper.SelectByComSumDay(createdDay));
        moneys.put("commoditySumHour",transactionMapper.SelectByComSumHour(created,firstDayOfThisMonth));
        moneys.put("commoditySumYear",transactionMapper.SelectByComSumYear(created,firstDayOfYear));
        moneys.put("commodityMoneyDay",transactionMapper.SelectByComPriceOneDay(createdDay));
        moneys.put("commodityMoneyHour",transactionMapper.SelectByComPriceHour(created,firstDayOfThisMonth));
        moneys.put("commodityMoneyYear",transactionMapper.SelectByComPriceYear(created,firstDayOfYear));


        return moneys;
    }

    //查询在comtrans表中,单个商品的销售记录
    @PostMapping("/comtransAll")
    public List<ComTrans> selectComTransAll(int comId){
        return transactionMapper.SelectToComTransAll(comId);
    }

    //查询在comtrans表中,单个商品的销售记录  -->筛选时间
    @PostMapping("/comtransAllByDate")
    public List<ComTrans> selectComTransAllByDate(int comId,LocalDateTime date1,LocalDateTime date2){
        System.out.println("comId = " + comId + ", date1 = " + date1 + ", date2 = " + date2);
        if (date1!=null || date2!=null){
            return transactionMapper.SelectToComTransAllByDate(comId,date1,date2);
        }
        return transactionMapper.SelectToComTransAll(comId);
    }
}
